﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

// 订单明细类
class OrderDetail
{
    public string ProductName { get; set; }
    public double Price { get; set; }
    public int Quantity { get; set; }

    public OrderDetail() { }

    public OrderDetail(string productName, double price, int quantity)
    {
        ProductName = productName;
        Price = price;
        Quantity = quantity;
    }

    public override string ToString()
    {
        return $"商品名称：{ProductName}，单价：{Price}，数量：{Quantity}";
    }
}

// 订单类
class Order
{
    public int OrderId { get; set; }
    public string Customer { get; set; }
    public List<OrderDetail> Details { get; set; }

    public double TotalAmount => Details.Sum(detail => detail.Price * detail.Quantity);

    public Order() { }

    public Order(int orderId, string customer, List<OrderDetail> details)
    {
        OrderId = orderId;
        Customer = customer;
        Details = details;
    }

    public override bool Equals(object obj)
    {
        var order = obj as Order;
        return order != null && OrderId == order.OrderId;
    }

    public override string ToString()
    {
        return $"订单号：{OrderId}，客户：{Customer}，订单总金额：{TotalAmount}";
    }
}

// 订单服务类
class OrderService
{
    private List<Order> orders = new List<Order>();

    public void AddOrder(Order order)
    {
        if (orders.Contains(order))
        {
            throw new InvalidOperationException("订单已存在，无法重复添加！");
        }
        orders.Add(order);
    }

    public void RemoveOrder(int orderId)
    {
        Order orderToRemove = orders.FirstOrDefault(order => order.OrderId == orderId);
        if (orderToRemove == null)
        {
            throw new InvalidOperationException("订单不存在，无法删除！");
        }
        orders.Remove(orderToRemove);
    }

    public List<Order> QueryOrders(Func<Order, bool> predicate)
    {
        return orders.Where(predicate).OrderBy(order => order.TotalAmount).ToList();
    }

    public void SortOrders(Comparison<Order> comparison)
    {
        orders.Sort(comparison);
    }

   
}

[TestClass]
public class OrderServiceTest
{
    [TestMethod]
    public void 测试添加()
    {
        OrderService orderService = new OrderService();
        Order order = new Order(1, "Customer1", new List<OrderDetail>());
        orderService.AddOrder(order);
        Assert.AreEqual(1, orderService.QueryOrders(o => o.Equals(order)).Count);
    }

    [TestMethod]
    [ExpectedException(typeof(InvalidOperationException))]
    public void TestAddDuplicateOrder()
    {
        OrderService orderService = new OrderService();
        Order order = new Order(1, "Customer1", new List<OrderDetail>());
        orderService.AddOrder(order);
        orderService.AddOrder(order);
    }

    [TestMethod]
    public void 测试移除()
    {
        OrderService orderService = new OrderService();
        Order order = new Order(1, "Customer1", new List<OrderDetail>());
        orderService.AddOrder(order);
        orderService.RemoveOrder(1);
        Assert.AreEqual(0, orderService.QueryOrders(o => o.Equals(order)).Count);
    }

    [TestMethod]
    [ExpectedException(typeof(InvalidOperationException))]
    public void 测试()
    {
        OrderService orderService = new OrderService();
        orderService.RemoveOrder(1);
    }

  
}
